home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / wrapper.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-01-02  |  4.9 KB  |  180 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20. /*
  21.   wrapper.cpp
  22.   ------------------------------------------------------------------------
  23.   Implementations of malloc(), free(), etc. in terms of hoard.
  24.   This lets us link in hoard in place of the stock malloc
  25.   (useful to test fragmentation).
  26.   ------------------------------------------------------------------------
  27.   @(#) $Id: wrapper.cpp,v 1.23 2000/02/28 17:42:08 emery Exp $
  28.   ------------------------------------------------------------------------
  29.   Emery Berger                    | <http://www.cs.utexas.edu/users/emery>
  30.   Department of Computer Sciences |             <http://www.cs.utexas.edu>
  31.   University of Texas at Austin   |                <http://www.utexas.edu>
  32.   ========================================================================
  33. */
  34.  
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #ifndef WIN32
  38. #include <strings.h>
  39. #endif
  40. #include "config.h"
  41.  
  42. #if USE_PRIVATE_HEAPS
  43. #include "privateheap.h"
  44. #define HEAPTYPE privateHeap
  45. #else
  46. #include "threadheap.h"
  47. #define HEAPTYPE threadHeap
  48. #endif
  49.  
  50. #include "processheap.h"
  51. #include "wrapper.h"
  52. #include "arch-specific.h"
  53.  
  54. static wrapper TheWrapper;
  55. char * wrapper::buf;
  56. processHeap * wrapper::TheRealAllocator;
  57. hoardLockType wrapper::_lock;
  58.  
  59.  
  60. // The Crystal Hoard will not over-ride new and delete, since they will in turn call
  61. // into the C-Runtime.  We want to preserve the native new and delete behaviour, which
  62. // on Solaris involves throwing exceptions on failure instead of returning NULL.
  63. #if !defined(CRYSTAL_HOARD)
  64. void * operator new (size_t size)
  65. {
  66.   return malloc (size);
  67. }
  68.  
  69. void * operator new[] (size_t size)
  70. {
  71.   return malloc (size);
  72. }
  73.  
  74. void operator delete (void * ptr)
  75. {
  76.   free (ptr);
  77. }
  78.  
  79. void operator delete[] (void * ptr)
  80. {
  81.   free (ptr);
  82. }
  83. #endif
  84.  
  85. extern "C" void * malloc (size_t sz)
  86. {
  87.   processHeap * pHeap = TheWrapper.TheAllocator();
  88.   void * addr = pHeap->getHeap(pHeap->getHeapIndex()).malloc (sz);
  89.   return addr;
  90. }
  91.  
  92. extern "C" void * calloc (size_t nelem, size_t elsize)
  93. {
  94. #if !defined(CRYSTAL_HOARD)
  95.   processHeap * pHeap = TheWrapper.TheAllocator();
  96.   void * ptr = pHeap->getHeap(pHeap->getHeapIndex()).malloc (nelem * elsize);
  97. #else
  98.   // Call malloc() indirectly, in case the loading application is also over-riding malloc(),
  99.   // but not over-riding calloc.  This is the case in /bin/sh in Solaris 8.  If we were to
  100.   // call threadHeap::malloc() directly, heap corruption would result in /bin/sh
  101.   void * ptr = malloc(nelem * elsize);
  102. #endif
  103.  
  104.   // Zero out the malloc'd block.
  105.   if ( ptr != NULL )
  106.     memset (ptr, 0, nelem * elsize);
  107.   return ptr;
  108. }
  109.  
  110. extern "C" void free (void * ptr)
  111. {
  112.   processHeap * pHeap = TheWrapper.TheAllocator();
  113. #if USE_PRIVATE_HEAPS
  114.   pHeap->getHeap(pHeap->getHeapIndex()).free(ptr);
  115. #else
  116.   pHeap->free (ptr);
  117. #endif
  118. }
  119.  
  120.  
  121. extern "C" void * memalign (size_t alignment, size_t size)
  122. {
  123.   processHeap * pHeap = TheWrapper.TheAllocator();
  124.   void * addr = pHeap->getHeap(pHeap->getHeapIndex()).memalign (alignment, size);
  125.   return addr;
  126. }
  127.  
  128.  
  129. extern "C" void * valloc (size_t size)
  130. {
  131.   return memalign (hoardGetPageSize(), size);
  132. }
  133.  
  134.  
  135. extern "C" void * realloc (void * ptr, size_t sz)
  136. {
  137.   if (ptr == NULL) {
  138.     return malloc (sz);
  139.   }
  140.   if (sz == 0) {
  141.     free (ptr);
  142.     return NULL;
  143.   }
  144.  
  145.   // Allocate a new block of size sz.
  146.   void * buf = malloc (sz);
  147.  
  148.   // Crystal-Decisions bug fix.
  149.   // According to the man page, if the allocation fails realloc should return
  150.   // NULL, but leave the memory pointed to by the original ptr intact.
  151.   // Hopefully, the caller knows this, and won't leak the original pointer.
  152.   if ( buf == NULL )
  153.     return NULL;
  154.  
  155.   // Find out how large the original object was.
  156.  
  157.   size_t objSize = HEAPTYPE::objectSize (ptr);
  158.  
  159.   // Copy the contents of the original object
  160.   // up to the size of the new block.
  161.  
  162.   size_t minSize = (objSize < sz) ? objSize : sz;
  163.   memcpy (buf, ptr, minSize);
  164.  
  165.   // Free the old block.
  166.  
  167.   free (ptr);
  168.  
  169.   // Return a pointer to the new one.
  170.  
  171.   return buf;
  172. }
  173.  
  174.  
  175. extern "C" void malloc_stats (void)
  176. {
  177.   TheWrapper.TheAllocator()->stats();
  178. }
  179.  
  180.